* msg:exiftags => msg:metadata (exiftags no longer exists)
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 *
8 */
9 if( !defined( 'MEDIAWIKI' ) )
10 die();
11
12 require_once( 'Image.php' );
13
14 /**
15 * Special handling for image description pages
16 * @package MediaWiki
17 */
18 class ImagePage extends Article {
19
20 /* private */ var $img; // Image object this page is shown for
21
22 function view() {
23 global $wgUseExternalEditor, $wgOut ;
24
25 $this->img = new Image( $this->mTitle );
26
27 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
28 if( $this->img->exists() ) {
29 $this->showTOC();
30 }
31 $this->openShowImage();
32
33 # No need to display noarticletext, we use our own message, output in openShowImage()
34 if( $this->getID() ) {
35 Article::view();
36 } else {
37 # Just need to set the right headers
38 $wgOut->setArticleFlag( true );
39 $wgOut->setRobotpolicy( 'index,follow' );
40 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
41 $wgOut->addMetaTags();
42 $this->viewUpdates();
43 }
44
45 $this->closeShowImage();
46 $this->imageHistory();
47 $this->imageLinks();
48 if( $this->img->exists() ) {
49 $this->showEXIFdata();
50 }
51 } else {
52 Article::view();
53 }
54 }
55
56 function showTOC() {
57 global $wgOut, $wgShowEXIF, $wgLang;
58 $r = '<ul id="filetoc">
59 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
60 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
61 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>' .
62 ($wgShowEXIF ? '<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') .
63 '</ul>';
64 $wgOut->addHTML( $r );
65 }
66
67 function showEXIFdata() {
68 global $wgOut, $wgShowEXIF;
69 if( !$wgShowEXIF ) {
70 return;
71 }
72
73 # Get the EXIF data
74 $exif = $this->img->getExifData();
75 if( count ( $exif ) == 0 ) {
76 return; # No EXIF data available
77 }
78
79 # Create the table
80 $r = '<h2 id="metadata">'. htmlspecialchars( wfMsg( 'metadata' ) ) . "</h2>\n";
81 $r .= "<table class='metadata'>\n" ;
82 $n = 0;
83 foreach( $exif as $k => $v ) {
84 if( $n % 2 == 0 ) {
85 $r .= '<tr>';
86 }
87 $r .= '<th>' . wfMsg( 'exif-' . strtolower( $k ) ) . "</th>\n";
88 $r .= '<td>' . htmlspecialchars( $v ) . "</td>\n";
89 if ( $n % 2 == 1 ) {
90 $r .= "</tr>\n";
91 } else {
92 $r .= "<td class='spacer'>&nbsp;</td>\n";
93 }
94 $n++;
95 }
96 if ( $n % 2 == 1 ) {
97 $r .= "<th></th><td></td></tr>\n";
98 }
99
100 $wgOut->addHTML( $r . "</table>\n" );
101 }
102
103 function openShowImage()
104 {
105 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
106 $wgUseImageResize, $wgRepositoryBaseUrl,
107 $wgUseExternalEditor, $wgServer;
108 $full_url = $this->img->getViewURL();
109 $anchoropen = '';
110 $anchorclose = '';
111
112 if( $wgUser->getOption( 'imagesize' ) == '' ) {
113 $sizeSel = User::getDefaultOption( 'imagesize' );
114 } else {
115 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
116 }
117 if( !isset( $wgImageLimits[$sizeSel] ) ) {
118 $sizeSel = User::getDefaultOption( 'imagesize' );
119 }
120 $max = $wgImageLimits[$sizeSel];
121 $maxWidth = $max[0];
122 $maxHeight = $max[1];
123 $sk = $wgUser->getSkin();
124
125 if ( $this->img->exists() ) {
126 if ( $this->img->getType() ) {
127 # image
128 $width = $this->img->getWidth();
129 $height = $this->img->getHeight();
130 # "Download high res version" link below the image
131 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
132 if ( $width > $maxWidth ) {
133 $height = floor( $height * $maxWidth / $width );
134 $width = $maxWidth;
135 }
136 if ( $height > $maxHeight ) {
137 $width = floor( $width * $maxHeight / $height );
138 $height = $maxHeight;
139 }
140 if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
141 if( $wgUseImageResize ) {
142 $thumbnail = $this->img->getThumbnail( $width );
143 $url = $thumbnail->getUrl();
144 } else {
145 # No resize ability? Show the full image, but scale
146 # it down in the browser so it fits on the page.
147 $url = $full_url;
148 }
149 $anchoropen = "<a href=\"{$full_url}\">";
150 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
151 } else {
152 $url = $full_url;
153 }
154 $s = '<div class="fullImageLink" id="file">' . $anchoropen .
155 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
156 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
157 } else {
158 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img->getName(),'' ) . '</div>';
159 }
160 $wgOut->addHTML( $s );
161 if($this->img->fromSharedDirectory) {
162 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
163 if($wgRepositoryBaseUrl) {
164 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey()));
165 }
166 $sharedtext.="</div>";
167 $wgOut->addWikiText($sharedtext);
168 }
169
170 } else {
171 # Image does not exist
172 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
173 }
174 }
175
176 function getUploadUrl() {
177 global $wgServer;
178 $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
179 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
180 }
181
182
183 function uploadLinksBox()
184 {
185 global $wgUser,$wgOut;
186 $sk = $wgUser->getSkin();
187 $wgOut->addHTML( '<br /><ul><li>' );
188 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
189 $wgOut->addHTML( '</li><li>' );
190 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
191 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
192 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
193 $wgOut->addHTML( '</li></ul>' );
194 }
195
196 function closeShowImage()
197 {
198 # For overloading
199
200 }
201
202 /**
203 * If the page we've just displayed is in the "Image" namespace,
204 * we follow it with an upload history of the image and its usage.
205 */
206 function imageHistory()
207 {
208 global $wgUser, $wgOut, $wgUseExternalEditor;
209
210 $sk = $wgUser->getSkin();
211
212 $line = $this->img->nextHistoryLine();
213
214 if ( $line ) {
215 $list =& new ImageHistoryList( $sk );
216 $s = $list->beginImageHistoryList() .
217 $list->imageHistoryLine( true, $line->img_timestamp,
218 $this->mTitle->getDBkey(), $line->img_user,
219 $line->img_user_text, $line->img_size, $line->img_description );
220
221 while ( $line = $this->img->nextHistoryLine() ) {
222 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
223 $line->oi_archive_name, $line->img_user,
224 $line->img_user_text, $line->img_size, $line->img_description );
225 }
226 $s .= $list->endImageHistoryList();
227 } else { $s=''; }
228 $wgOut->addHTML( $s );
229
230 # Exist check because we don't want to show this on pages where an image
231 # doesn't exist along with the noimage message, that would suck. -ævar
232 if( $wgUseExternalEditor && $this->img->exists() ) {
233 $this->uploadLinksBox();
234 }
235
236 }
237
238 function imageLinks()
239 {
240 global $wgUser, $wgOut;
241
242 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
243
244 $dbr =& wfGetDB( DB_SLAVE );
245 $page = $dbr->tableName( 'page' );
246 $imagelinks = $dbr->tableName( 'imagelinks' );
247
248 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
249 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id"
250 . " LIMIT 500"; # quickie emergency brake
251 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
252
253 if ( 0 == $dbr->numRows( $res ) ) {
254 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
255 return;
256 }
257 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
258
259 $sk = $wgUser->getSkin();
260 while ( $s = $dbr->fetchObject( $res ) ) {
261 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
262 $link = $sk->makeKnownLinkObj( $name, "" );
263 $wgOut->addHTML( "<li>{$link}</li>\n" );
264 }
265 $wgOut->addHTML( "</ul>\n" );
266 }
267
268 function delete()
269 {
270 global $wgUser, $wgOut, $wgRequest;
271
272 $confirm = $wgRequest->getBool( 'wpConfirmB' );
273 $image = $wgRequest->getVal( 'image' );
274 $oldimage = $wgRequest->getVal( 'oldimage' );
275
276 # Only sysops can delete images. Previously ordinary users could delete
277 # old revisions, but this is no longer the case.
278 if ( !$wgUser->isAllowed('delete') ) {
279 $wgOut->sysopRequired();
280 return;
281 }
282 if ( wfReadOnly() ) {
283 $wgOut->readOnlyPage();
284 return;
285 }
286
287 # Better double-check that it hasn't been deleted yet!
288 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
289 if ( ( !is_null( $image ) )
290 && ( '' == trim( $image ) ) ) {
291 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
292 return;
293 }
294
295 $this->img = new Image( $this->mTitle );
296
297 # Deleting old images doesn't require confirmation
298 if ( !is_null( $oldimage ) || $confirm ) {
299 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
300 $this->doDelete();
301 } else {
302 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
303 }
304 return;
305 }
306
307 if ( !is_null( $image ) ) {
308 $q = '&image=' . urlencode( $image );
309 } else if ( !is_null( $oldimage ) ) {
310 $q = '&oldimage=' . urlencode( $oldimage );
311 } else {
312 $q = '';
313 }
314 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
315 }
316
317 function doDelete()
318 {
319 global $wgOut, $wgUser, $wgContLang, $wgRequest;
320 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
321 $fname = 'ImagePage::doDelete';
322
323 $reason = $wgRequest->getVal( 'wpReason' );
324 $oldimage = $wgRequest->getVal( 'oldimage' );
325
326 $dbw =& wfGetDB( DB_MASTER );
327
328 if ( !is_null( $oldimage ) ) {
329 if ( strlen( $oldimage ) < 16 ) {
330 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
331 return;
332 }
333 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
334 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
335 return;
336 }
337
338 # Invalidate description page cache
339 $this->mTitle->invalidateCache();
340
341 # Squid purging
342 if ( $wgUseSquid ) {
343 $urlArr = Array(
344 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
345 $wgInternalServer.$this->mTitle->getFullURL()
346 );
347 wfPurgeSquidServers($urlArr);
348 }
349 $this->doDeleteOldImage( $oldimage );
350 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
351 $deleted = $oldimage;
352 } else {
353 $image = $this->mTitle->getDBkey();
354 $dest = wfImageDir( $image );
355 $archive = wfImageDir( $image );
356
357 # Delete the image file if it exists; due to sync problems
358 # or manual trimming sometimes the file will be missing.
359 $targetFile = "{$dest}/{$image}";
360 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
361 # If the deletion operation actually failed, bug out:
362 $wgOut->fileDeleteError( $targetFile );
363 return;
364 }
365 $dbw->delete( 'image', array( 'img_name' => $image ) );
366 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
367
368 # Purge archive URLs from the squid
369 $urlArr = Array();
370 while ( $s = $dbw->fetchObject( $res ) ) {
371 $this->doDeleteOldImage( $s->oi_archive_name );
372 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
373 }
374
375 # And also the HTML of all pages using this image
376 $linksTo = $this->img->getLinksTo();
377 if ( $wgUseSquid ) {
378 $u = SquidUpdate::newFromTitles( $linksTo, $urlArr );
379 array_push( $wgPostCommitUpdateList, $u );
380 }
381
382 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
383
384 # Image itself is now gone, and database is cleaned.
385 # Now we remove the image description page.
386
387 $article = new Article( $this->mTitle );
388 $article->doDeleteArticle( $reason ); # ignore errors
389
390 # Invalidate parser cache and client cache for pages using this image
391 # This is left until relatively late to reduce lock time
392 Title::touchArray( $linksTo );
393
394 /* Delete thumbnails and refresh image metadata cache */
395 $this->img->purgeCache();
396
397
398 $deleted = $image;
399 }
400
401 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
402 $wgOut->setRobotpolicy( 'noindex,nofollow' );
403
404 $sk = $wgUser->getSkin();
405 $loglink = $sk->makeKnownLinkObj(
406 Title::makeTitle( NS_SPECIAL, 'Log/delete' ),
407 wfMsg( 'deletionlog' ) );
408
409 $text = wfMsg( 'deletedtext', $deleted, $loglink );
410
411 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
412 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
413 }
414
415 function doDeleteOldImage( $oldimage )
416 {
417 global $wgOut;
418
419 $name = substr( $oldimage, 15 );
420 $archive = wfImageArchiveDir( $name );
421
422 # Delete the image if it exists. Sometimes the file will be missing
423 # due to manual intervention or weird sync problems; treat that
424 # condition gracefully and continue to delete the database entry.
425 # Also some records may end up with an empty oi_archive_name field
426 # if the original file was missing when a new upload was made;
427 # don't try to delete the directory then!
428 #
429 $targetFile = "{$archive}/{$oldimage}";
430 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
431 # If we actually have a file and can't delete it, throw an error.
432 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
433 } else {
434 # Log the deletion
435 $log = new LogPage( 'delete' );
436 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
437 }
438 }
439
440 function revert()
441 {
442 global $wgOut, $wgRequest, $wgUser;
443 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
444
445 $oldimage = $wgRequest->getText( 'oldimage' );
446 if ( strlen( $oldimage ) < 16 ) {
447 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
448 return;
449 }
450 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
451 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
452 return;
453 }
454
455 if ( wfReadOnly() ) {
456 $wgOut->readOnlyPage();
457 return;
458 }
459 if( $wgUser->isAnon() ) {
460 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
461 return;
462 }
463 if ( ! $this->mTitle->userCanEdit() ) {
464 $wgOut->sysopRequired();
465 return;
466 }
467 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
468 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
469 return;
470 }
471 $name = substr( $oldimage, 15 );
472
473 $dest = wfImageDir( $name );
474 $archive = wfImageArchiveDir( $name );
475 $curfile = "{$dest}/{$name}";
476
477 if ( ! is_file( $curfile ) ) {
478 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
479 return;
480 }
481 $oldver = wfTimestampNow() . "!{$name}";
482
483 $dbr =& wfGetDB( DB_SLAVE );
484 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
485
486 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
487 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
488 return;
489 }
490 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
491 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
492 }
493
494 # Record upload and update metadata cache
495 $img = Image::newFromName( $name );
496 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
497
498 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
499 $wgOut->setRobotpolicy( 'noindex,nofollow' );
500 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
501
502 $descTitle = $img->getTitle();
503 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
504 }
505 }
506
507 /**
508 * @todo document
509 * @package MediaWiki
510 */
511 class ImageHistoryList {
512 function ImageHistoryList( &$skin ) {
513 $this->skin =& $skin;
514 }
515
516 function beginImageHistoryList() {
517 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
518 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
519 return $s;
520 }
521
522 function endImageHistoryList() {
523 $s = "</ul>\n";
524 return $s;
525 }
526
527 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
528 global $wgUser, $wgLang, $wgContLang, $wgTitle;
529
530 $datetime = $wgLang->timeanddate( $timestamp, true );
531 $del = wfMsg( 'deleteimg' );
532 $delall = wfMsg( 'deleteimgcompletely' );
533 $cur = wfMsg( 'cur' );
534
535 if ( $iscur ) {
536 $url = Image::imageUrl( $img );
537 $rlink = $cur;
538 if ( $wgUser->isAllowed('delete') ) {
539 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
540 '&action=delete' );
541 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
542
543 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
544 } else {
545 $dlink = $del;
546 }
547 } else {
548 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
549 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
550 $token = urlencode( $wgUser->editToken( $img ) );
551 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
552 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
553 urlencode( $img ) . "&wpEditToken=$token" );
554 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
555 $del, 'action=delete&oldimage=' . urlencode( $img ) .
556 "&wpEditToken=$token" );
557 } else {
558 # Having live active links for non-logged in users
559 # means that bots and spiders crawling our site can
560 # inadvertently change content. Baaaad idea.
561 $rlink = wfMsg( 'revertimg' );
562 $dlink = $del;
563 }
564 }
565 if ( 0 == $user ) {
566 $userlink = $usertext;
567 } else {
568 $userlink = $this->skin->makeLinkObj(
569 Title::makeTitle( NS_USER, $usertext ),
570 $usertext );
571 }
572 $nbytes = wfMsg( 'nbytes', $size );
573 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
574
575 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
576 . " . . {$userlink} ({$nbytes})";
577
578 $s .= $this->skin->commentBlock( $description, $wgTitle );
579 $s .= "</li>\n";
580 return $s;
581 }
582
583 }
584
585
586 ?>